package com.hnkywl.sts.service.zjgl;

import com.hnkywl.sts.dao.cwgl.KhzhsfmxDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.zjgl.PjglDao;
import com.hnkywl.sts.dao.zjgl.PjdrDao;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Pjdr;
import com.hnkywl.sts.service.organ.KhhService;
import com.hnkywl.sts.service.cwgl.KhzhsfmxService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sfgl
 * 类描述:   付款审核服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class PjglService {
    private PjglDao pjglDao;
    private GsjgDao gsjgDao;
    private KhzhsfmxService khzhsfmxService;
    private YgService ygService;
    private KhhService khhService;
    private KhzhsfmxDao khzhsfmxDao;
    private PjdrDao pjdrDao;

    /**
     *
     * @param pjsflxYype     票据收付类型
     * @param pjlxType       票据类型
     * @param khztType        考核状态
     * @param gsId           公司ID
     * @param page
     * @param rows
     * @return
     */
    @Transactional
    public String findAllPjgl(String pjhm,Long sfkId,Integer pjsflxYype,Integer pjlxType,Integer khztType,Long gsId,String gcMc,String khhMc,Date startDate,Date endDate,Integer zt,int page,int  rows) {
        Long nbgsIds = null;
        Long khhIds = null;
        if(gsId !=null && gcMc != null && gcMc.equals(gsjgDao.get(gsId).getJc())){
           nbgsIds = gsId;
        }
        Page<Pj> pjglPage = this.pjglDao.findPjglList(pjhm,sfkId,pjsflxYype, pjlxType, khztType, gcMc,nbgsIds,khhMc,startDate,endDate,zt,page, rows);
        List<Pj> pjglList = pjglPage.getResult() == null ? new ArrayList<Pj>() : pjglPage.getResult();
        String pjglJson;
        if(pjsflxYype==0){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "pjlx", "pjhm", "khzt",
                    "yscpyh","dqr","dqje" ,"cprq","cpr","skr" ,"jpdw.qc","yslx","syrq","bz","status","sk.bm");
            Page<Pj> ysSumPage = this.pjglDao.findPjglList(pjhm,sfkId,pjsflxYype, pjlxType, khztType, gcMc,nbgsIds,khhMc,startDate,endDate,zt,1, -1);
            List<Pj> ysSumList = ysSumPage.getResult() == null ? new ArrayList<Pj>() : ysSumPage.getResult();
            Double dqje = 0d;
            if (ysSumList.size() != 0) {
                for (Pj pj : ysSumList) {
                    dqje += Double.valueOf(pj.getDqje().toString());
                }
                BigDecimal bigDecimal_total_dqje = new BigDecimal(dqje).setScale(2, BigDecimal.ROUND_HALF_UP);
                return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "],\"footer\":[{\"nbgs_jc\":\"合计:\",\"dqje\":\"" + bigDecimal_total_dqje + "\"}]}";
            } else {
                return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
            }
        }else if(pjsflxYype==1){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "pjlx", "pjhm",
                    "yfcpyh.qc","dqr","bzj","cprq","dqje" ,"spr.jc","yflx","syrq","bz","status","sfk.bm","sfk.sfkrq");
            Page<Pj> ysSumPage = this.pjglDao.findPjglList(pjhm,sfkId,pjsflxYype, pjlxType, khztType, gcMc,nbgsIds,khhMc,startDate,endDate,zt,1, -1);
            List<Pj> ysSumList = ysSumPage.getResult() == null ? new ArrayList<Pj>() : ysSumPage.getResult();
            Double dqje = 0d;
            if (ysSumList.size() != 0) {
                for (Pj pj : ysSumList) {
                    dqje += Double.valueOf(pj.getDqje().toString());
                }
                BigDecimal bigDecimal_total_dqje = new BigDecimal(dqje).setScale(2, BigDecimal.ROUND_HALF_UP);
                return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "],\"footer\":[{\"nbgs_jc\":\"合计:\",\"dqje\":\"" + bigDecimal_total_dqje + "\"}]}";
            } else {
                return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
            }
        } else{
            pjglJson = StringUtils.EMPTY;
            return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
        }
//        return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
    }

    /**
     * 查询票据兑换明细
     * @param pjId 票据Id
     * @param page
     * @param rows
     * @return
     */
    @Transactional
    public String  findPjdhmx(Long pjId,int page,int  rows) {
        Page<Pj> pjglPage = this.pjglDao.findPjdhmx(pjId,page, rows);
        List<Pj> pjglList = pjglPage.getResult() == null ? new ArrayList<Pj>() : pjglPage.getResult();
        String    pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "pjlx", "pjhm",
                    "yfcpyh.qc","dqr","bzj","dqje" ,"spr.jc","yflx","syrq","bz","status");
        return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
    }


    /**
     *
     * @param pjsflxYype     票据收付类型
     * @param
     * @param
     * @param
     * @param page
     * @param rows
     * @return
     */
    @Transactional
    public String findPjglWsy(Long gsId,Integer pjsflxYype,String khhMc,String pjhm, int page,int  rows) {
        Page<Pj> pjglPage = this.pjglDao.findwsyPjglList(gsId,pjsflxYype,khhMc,pjhm, page, rows);
        List<Pj> pjglList = pjglPage.getResult() == null ? new ArrayList<Pj>() : pjglPage.getResult();
        String pjglJson;
        if(pjsflxYype==0){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "pjlx", "pjhm", "khzt",
                    "yscpyh","dqr","dqje" ,"cpr","skr" ,"jpdw.jc","yslx","syrq","bz","status");
        }else if(pjsflxYype==1){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "pjlx", "pjhm",
                    "yfcpyh.qc","dqr","bzj","dqje" ,"spr.jc","yflx","syrq","bz","status");
        } else{
            pjglJson = null;
        }

        return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
    }


    @Transactional
    public String findPjgl(List<Pj> pjList) {
       String pjglJson = BaseUtils.fetchCollectionToJson(pjList, "id", "pjlx","pjhm",
                    "dqr","dqje");
        return "{\"total\":" + pjList.size()+ ",\"rows\":[" + pjglJson + "]}";
    }

    /**
     * 保存票据管理应付票据
     * @param pj 票据管理实体
     * @return
     */
    @Transactional
    public void savePjgl(Pj pj,Integer pjsflx) {
        pj.setKhzt(0);
        pj.setDateCreated(new Date());
        pj.setCreateUser(YgService.getCurrentYg());
        pj.setCreateOrgan(YgService.getCurrentYg().getZggs());
        pj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        pj.setLastUpdated(pj.getDateCreated());
        pj.setPjsflx(pjsflx);
        pj.setStatus(1);
        if(pj.getYfcpyh() !=null && pj.getYfcpyh().getId() !=null){
        pj.setYfcpyh(khhService.findKhhById(pj.getYfcpyh().getId()));
        }
        this.pjglDao.save(pj);
    }


    /**
     * 票据兑换
     * @param dhpj 兑换票据管理实体
     * @return
     */
    @Transactional
    public void pjdh(Long pjId,Pj dhpj,Integer pjsflx) {
        Pj pj = this.findPjDetail(pjId);
        for(Pj p :dhpj.getPjdhList()){
            if(p.getYfcpyh().getId()==null)
                p.setYfcpyh(null);
            p.setKhzt(0);
            p.setDateCreated(new Date());
            p.setCreateUser(YgService.getCurrentYg());
            p.setCreateOrgan(YgService.getCurrentYg().getZggs());
            p.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            p.setLastUpdated(p.getDateCreated());
            p.setPjsflx(pjsflx);
            p.setStatus(1);
            p.setDhpj(pj);
            this.pjglDao.save(p);
        }
        pj.setPjdhList(dhpj.getPjdhList());
        pj.setStatus(6);
        pj.setLastUpdated(new Date());
        this.pjglDao.save(pj);
    }


    /**
     * 票据取消兑换
     * @param
     */
    @Transactional
    public void pjglQxdh(Pj j) {
        List<Pj> pjList = j.getPjdhList();
        if(pjList.size()>0){
            for(Pj pj :pjList){
                pj.setDeleted(1);
                pj.setLastUpdated(new Date());
                this.pjglDao.save(pj);
            }
        }
        j.setSfk(j.getSfk());
        j.setStatus(1);
        this.pjglDao.save(j);
    }



    @Transactional
    public void savePjgl(Pj pj) {
        pj.setLastUpdated(new Date());
        this.pjglDao.save(pj);
    }

    /**
     * 票据贴现
     * @param id 票据id集合
     * @return
     */
    @Transactional
    public void Pjtx(String  id,String txlxzc,Long sqrId,Long yhmcId,String bz) {
        String[] ids = id.split(",");
        Pj pj = this.findPjDetail(Long.parseLong(ids[0]));
        //票据贴现前期准备工作  1.计算总的贴现金额 2.判断贴现的票据类型是不是一样
        //                      3.贴现必须是一家公司
        Long dbgsId =  pj.getNbgs().getId(); //做对比的票据公司Id
        Integer dbpjlx =  pj.getPjlx(); //做对比的票据票据类型
        Double countDqje = 0d;//总的到期金额
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            if(j.getNbgs().getId() != dbgsId){
                throw  new RuntimeException("请选择同一家公司进行贴现");
            }
            if(!j.getPjlx().toString().equals(dbpjlx.toString())){
                throw  new RuntimeException("请选择同一种票据类型进行贴现");
            }
            countDqje += Double.parseDouble(j.getDqje().toString());
        }

        //生成一个票据贴现承兑导入数据
        Pjdr pjdr = new Pjdr();
        pjdr.setRzyh(khhService.findKhhById(yhmcId));
        pjdr.setNbgs(pj.getNbgs());
        pjdr.setPjsflx(pj.getPjsflx());
        pjdr.setPjlx(pj.getPjlx());
        pjdr.setDqje(countDqje);
        pjdr.setStatus(2);
        pjdr.setCreateUser(YgService.getCurrentYg());
        pjdr.setCreateOrgan(YgService.getCurrentYg().getZggs());
        pjdr.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        pjdr.setDateCreated(new Date());
        pjdr.setLastUpdated(pjdr.getDateCreated());
        pjdr.setBz(bz);
        pjdrDao.save(pjdr);

        //将pj的金额贴现增加到银行存款内
            Khzhsfmx khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(pj.getNbgs());
            khzhsfmx.setType(1);
            khzhsfmx.setRzyh(khhService.findKhhById(yhmcId));//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            khzhsfmx.setJe(Double.parseDouble(countDqje.toString()));
            khzhsfmx.setPjdr(pjdr);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(7);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);

            //贴现息减少到银行存款内
            khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(pj.getNbgs());
            khzhsfmx.setType(2);
            khzhsfmx.setRzyh(khhService.findKhhById(yhmcId));//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            //对于贴现息的处理 可能存在问题
            khzhsfmx.setJe(Double.parseDouble(txlxzc));
            khzhsfmx.setPjdr(pjdr);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(8);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);

        //修改单个票据的状态
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            j.setSfk(j.getSfk());
            j.setStatus(2);
            j.setPjdr(pjdr);
            j.setLastUpdated(new Date());
            this.pjglDao.save(j);
        }
    }

    /**
     * 票据取消贴现
     * @param
     */
    @Transactional
    public void pjglQxtx(Pj j) {
        j.setSfk(j.getSfk());
        j.setStatus(1);
        this.pjglDao.save(j);
    }


    /**
     * 票据托收
     * @param id 票据id集合
     * @return
     */
    @Transactional
    public void Pjts(String  id,Long sqrId,Long yhmcId,String bz) {
        String[] ids = id.split(",");
        Pj pj = this.findPjDetail(Long.parseLong(ids[0]));
        //票据托收前期准备工作  1.计算总的托收金额 2.判断托收的票据类型是不是一样
        //                      3.托收必须是一家公司
        Long dbgsId =  pj.getNbgs().getId(); //做对比的票据公司Id
        Integer dbpjlx =  pj.getPjlx(); //做对比的票据票据类型
        Double countDqje = 0d;//总的到期金额
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            if(j.getNbgs().getId() != dbgsId){
                throw  new RuntimeException("请选择同一家公司进行贴现");
            }
            if(!j.getPjlx().toString().equals(dbpjlx.toString())){
                throw  new RuntimeException("请选择同一种票据类型进行贴现");
            }
            countDqje += Double.parseDouble(j.getDqje().toString());
        }

        //生成一个票据贴现承兑导入数据
        Pjdr pjdr = new Pjdr();
        pjdr.setNbgs(pj.getNbgs());
        pjdr.setRzyh(khhService.findKhhById(yhmcId));
        pjdr.setPjsflx(pj.getPjsflx());
        pjdr.setPjlx(pj.getPjlx());
        pjdr.setDqje(countDqje);
        pjdr.setStatus(3);
        pjdr.setCreateUser(YgService.getCurrentYg());
        pjdr.setCreateOrgan(YgService.getCurrentYg().getZggs());
        pjdr.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        pjdr.setDateCreated(new Date());
        pjdr.setLastUpdated(pjdr.getDateCreated());
        pjdr.setBz(bz);
        pjdrDao.save(pjdr);


        //将pj的金额贴现增加到银行存款内
            Khzhsfmx khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(pj.getNbgs());
            khzhsfmx.setType(1);
            khzhsfmx.setRzyh(khhService.findKhhById(yhmcId));//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            khzhsfmx.setJe(Double.parseDouble(countDqje.toString()));
            khzhsfmx.setPjdr(pjdr);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(7);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);

        //修改单个票据的状态
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            j.setSfk(j.getSfk());
            j.setStatus(3);
            j.setPjdr(pjdr);
            j.setLastUpdated(new Date());
            this.pjglDao.save(j);
        }
    }

    /**
     * 票据取消托收
     * @param
     */
    @Transactional
    public void pjglQxts(Pj j) {
        j.setSfk(j.getSfk());
        j.setStatus(1);
        this.pjglDao.save(j);
    }

    /**
     * 票据承兑
     * @param id 票据id集合
     * @return
     */
    @Transactional
    public void Pjcd(String  id,Long sqrId,Long yhmcId,String bz) {
        String[] ids = id.split(",");
        Pj pj = this.findPjDetail(Long.parseLong(ids[0]));
        //票据承兑前期准备工作  1.计算总的承兑金额 2.判断承兑的票据类型是不是一样
        //                      3.承兑必须是一家公司
        Long dbgsId =  pj.getNbgs().getId(); //做对比的票据公司Id
        Integer dbpjlx =  pj.getPjlx(); //做对比的票据票据类型
        Double countDqje = 0d;//总的到期金额
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            if(j.getNbgs().getId() != dbgsId){
                throw  new RuntimeException("请选择同一家公司进行承兑");
            }
            if(!j.getPjlx().toString().equals(dbpjlx.toString())){
                throw  new RuntimeException("请选择同一种票据类型进行承兑");
            }
            countDqje += Double.parseDouble(j.getDqje().toString());
        }

        //生成一个票据贴现承兑导入数据
        Pjdr pjdr = new Pjdr();
        pjdr.setNbgs(pj.getNbgs());
        pjdr.setRzyh(pj.getYfcpyh());
        pjdr.setPjsflx(pj.getPjsflx());
        pjdr.setPjlx(pj.getPjlx());
        pjdr.setDqje(countDqje);
        pjdr.setStatus(4);
        pjdr.setCreateUser(YgService.getCurrentYg());
        pjdr.setCreateOrgan(YgService.getCurrentYg().getZggs());
        pjdr.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        pjdr.setDateCreated(new Date());
        pjdr.setLastUpdated(pjdr.getDateCreated());
        pjdr.setBz(bz);
        pjdrDao.save(pjdr);

        //将pj的金额承兑增加到银行存款内
            Khzhsfmx khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(pj.getNbgs());
            khzhsfmx.setType(2);
            khzhsfmx.setRzyh(pj.getYfcpyh());//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            khzhsfmx.setJe(Double.parseDouble(countDqje.toString()));
            khzhsfmx.setPjdr(pjdr);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(5);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);

        //toDo
        for(String pjId :ids){
            Pj j = this.findPjDetail(Long.parseLong(pjId));
            j.setSfk(j.getSfk());
            j.setStatus(4);
            j.setLastUpdated(new Date());
            j.setPjdr(pjdr);
            this.pjglDao.save(j);
        }
    }


    /**
     * 票据取消贴现
     * @param
     */
    @Transactional
    public void pjglQxcd(Pj j) {
        j.setSfk(j.getSfk());
        j.setStatus(5);
        this.pjglDao.save(j);
        List<Khzhsfmx> khzhsfmxList = khzhsfmxDao.findKhzhsfmxByPj(j.getId());
        if(khzhsfmxList.size()>0){
            for(Khzhsfmx khzhsfmx :khzhsfmxList){
                khzhsfmx.setDeleted(1);
                khzhsfmx.setLastUpdated(new Date());
                khzhsfmxDao.save(khzhsfmx);
            }
        }
    }

   public  boolean  checkYspjhm(Long gsId,String pjhm){
       List<Pj> pjList = pjglDao.checkYspjhm(gsId,pjhm);
       if(pjList.size()>0){
           return false;
       }else{
           return true;
       }
   }





    /**
     * 更新应付账款票据管理
     * @param pj 票据管理实体
     * @return
     */
    @Transactional
    public void updatePjgl(Pj pj) {
        Pj clone = this.findPjDetail(pj.getId());
        Sfk sfk = clone.getSfk();
        Integer stat = clone.getStatus();
        clone = (Pj) ReflectionUtils.getInstance(clone, pj);
        clone.setLastUpdated(new Date());
        clone.setSfk(sfk);
        clone.setStatus(stat);
        this.pjglDao.save(clone);
  }

    /**
     * 删除应付票据
     * @param id 应付票据id
     * @return
     */
    @Transactional
    public void deletePjgl(Long id) {
        Pj pjgl = this.findPjDetail(id);
        if(pjgl.getPjsflx()==0){
            throw new RuntimeException("应收票据请删除关联收款申请");
        }
        if(pjgl.getPjsflx()==1 && pjgl.getDhpj() != null){
            throw new RuntimeException("该应付票据为兑换票据,请使用取消兑换删除");
        }
        pjgl.setLastUpdated(new Date());
        pjgl.setDeleted(1);
        this.pjglDao.save(pjgl);
    }
    /**
     * 查找票据
     * @param id 票据Id
     * @return
     */
    @Transactional
    public Pj findPjDetail(Long id) {
        return   this.pjglDao.get(id);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id,Integer pjsflxType, String value, Object field) {
        return this.pjglDao.checkUnique(id,pjsflxType, value, field);
    }


    @Transactional
    public List<Pj> findAllPjglNoPage(String pjhm,Long sfkId,Integer pjsflxYype,Integer pjlxType,Integer khztType,Long gsId,String gcMc,String khhMc,Date startDate,Date endDate,Integer zt,int page,int  rows) {
        Long nbgsIds = null;
        Long khhIds = null;
        if(gsId !=null && gcMc.equals(gsjgDao.get(gsId).getJc())){
            nbgsIds = gsId;
        }
        Page<Pj> pjglPage = this.pjglDao.findPjglList(pjhm,sfkId,pjsflxYype, pjlxType, khztType, gcMc,nbgsIds,khhMc,startDate,endDate,zt,page, rows);
        return  pjglPage.getResult() == null ? new ArrayList<Pj>() : pjglPage.getResult();
    }


    @Autowired
    public void setPjglDao(PjglDao pjglDao) {
        this.pjglDao = pjglDao;
    }
    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
    @Autowired
    public void setKhzhsfmxService(KhzhsfmxService khzhsfmxService) {
        this.khzhsfmxService = khzhsfmxService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setKhhService(KhhService khhService) {
        this.khhService = khhService;
    }
    @Autowired
    public void setKhzhsfmxDao(KhzhsfmxDao khzhsfmxDao) {
        this.khzhsfmxDao = khzhsfmxDao;
    }
    @Autowired
    public void setPjdrDao(PjdrDao pjdrDao) {
        this.pjdrDao = pjdrDao;
    }
}
